package com.ljt.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljt.exceptions.BusinessException;
import com.ljt.response.Result;
import com.ljt.response.ResultCode;
import com.ljt.system.entity.Department;
import com.ljt.system.entity.User;
import com.ljt.system.service.DepartmentService;
import com.ljt.system.service.DepartmentVoService;
import com.ljt.system.vo.DepartmentVo;
import com.ljt.system.vo.UserVo;
import com.ljt.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ljt
 * @since 2021-06-27
 */
@RestController
@RequestMapping("/department")
@Api(value = "系统部门模块", tags = "系统用户接口")
@CrossOrigin
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private DepartmentVoService departmentVoService;

    @ApiOperation(value = "部门列表", notes = "模糊查询部门信息")
    @GetMapping("/queryAllDeptByCondition")
    public Result queryAllDeptByCondition(@RequestParam(required = false,defaultValue = "1") Integer current,
                                         @RequestParam(required = false,defaultValue = "6") Integer size,@RequestParam String name) {

        Page<DepartmentVo> deptPage = new Page<>(current, size);
        deptPage = departmentVoService.queryAllDeptByCondition(deptPage,name);
        return Result.ok().data("deptPage", deptPage);
    }

    @GetMapping("/queryDeptHasCount")
    @ApiOperation(value = "获取部门信息",notes = "按部门分组查询部门信息和部门人数")
    public Result queryDeptHasCount() {
        List<Department> departments = departmentService.queryDeptHasCount();
        if (departments == null) {
            throw new BusinessException(ResultCode.DEPT_NOT_FOUND.getCode(),ResultCode.DEPT_NOT_FOUND.getMessage());
        }else {
            return Result.ok().data("departments",departments);
        }
    }

    @PostMapping("/insertDept")
    @ApiOperation(value = "添加部门信息",notes = "添加新部门")
    public Result insertDept(@RequestBody Department department){
        boolean i=departmentService.save(department);
        if (i){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    @GetMapping("/queryDeptById")
    @ApiOperation(value = "部门信息", notes = "根据id查询部门")
    public Result queryDeptById(int id) {
        Department dept = departmentService.getById(id);
        return Result.ok().data("dept", dept);
    }

    @PostMapping("/updateDeptById")
    @ApiOperation(value = "修改部门信息",notes = "修改部门信息")
    public Result updateByDeptId(@RequestBody Department department){
        boolean i=departmentService.updateById(department);
        if(i){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    @GetMapping("/deleteDeptById")
    @ApiOperation(value = "删除部门信息",notes = "删除部门信息")
    public Result deleteDeptById(@RequestParam(required = false,defaultValue = "1") int id){
        System.out.println("id："+id);
        boolean i=departmentService.removeById(id);
        if (i) {
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    @ApiOperation(value = "导出",produces="application/octet-stream")
    @GetMapping("/exportDepartmentToExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<Department> department = departmentService.list();
        ExcelUtil<Department> util = new ExcelUtil<>(Department.class);
        util.exportExcel(response,department , "部门数据");
    }

    @ApiOperation("导入")
    @PostMapping("/importInStockToExcel")
    public Result importExcel(@RequestBody MultipartFile file) throws Exception {
        ExcelUtil<Department> util = new ExcelUtil<>(Department.class);
        List<Department> departmentList = util.importExcel(file.getInputStream());
        boolean insertFlag = departmentService.saveBatch(departmentList);
        if (insertFlag){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
}

